package com.ruoyi.web.controller.user;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.util.FileUploadUtils;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.user.domain.Knowledge;
import com.ruoyi.user.service.IKnowledgeService;

/**
 * 健康知识 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-12-18
 */
@Controller
@RequestMapping("/knowledge")
public class KnowledgeController extends BaseController
{
    private String prefix = "knowledge";
	
	@Autowired
	private IKnowledgeService knowledgeService;
	
	@RequiresPermissions("knowledge:view")
	@GetMapping()
	public String knowledge()
	{
	    return prefix + "/knowledge";
	}
	
	/**
	 * 查询健康知识列表
	 */
	@RequiresPermissions("knowledge:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Knowledge knowledge)
	{
		startPage();
        List<Knowledge> list = knowledgeService.selectKnowledgeList(knowledge);
		return getDataTable(list);
	}
	
	/**
	 * 新增健康知识
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存健康知识
	 */
	@RequiresPermissions("knowledge:add")
	@Log(title = "健康知识", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Knowledge knowledge,MultipartFile file)
	{		
		String pic = null;
		try {
			// 获取跟目录
			File path = new File(ResourceUtils.getURL("classpath:").getPath());
			if (!path.exists()) {
				path = new File("");
			}
			// 如果上传目录为/static/app/img/,则可以如下获取
			File upload = new File("/home/find/web/img/");
			if (!upload.exists()) {
				upload.mkdirs();
			}
			if (!file.isEmpty()) {
				pic = FileUploadUtils.upload(upload.getAbsolutePath()+"/", file);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(pic!=null){
			knowledge.setPic("app/img/"+pic);
		}
		knowledge.setCreateTime(new Date());
		knowledge.setCreateBy("admin");
		return toAjax(knowledgeService.insertKnowledge(knowledge));
	}

	/**
	 * 修改健康知识
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Knowledge knowledge = knowledgeService.selectKnowledgeById(id);
		mmap.put("knowledge", knowledge);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存健康知识
	 */
	@RequiresPermissions("knowledge:edit")
	@Log(title = "健康知识", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Knowledge knowledge,MultipartFile file)
	{		
		String pic = null;
		File path = null;
		File upload = null;
		try {
			// 获取跟目录
			path = new File(ResourceUtils.getURL("classpath:").getPath());
			if (!path.exists()) {
				path = new File("");
			}
			// 如果上传目录为/static/app/img/,则可以如下获取
			upload = new File("/home/find/web/img/");
			if (!upload.exists()) {
				upload.mkdirs();
			}
			if (!file.isEmpty()) {
				pic = FileUploadUtils.upload(upload.getAbsolutePath()+"/", file);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(pic!=null){
			knowledge.setPic("app/img/"+pic);
		}
		knowledge.setUpdateTime(new Date());
		return toAjax(knowledgeService.updateKnowledge(knowledge));
	}
	
	/**
	 * 文件上传
	 */
	@PostMapping("/upload")
	@ResponseBody
	public HashMap<String, Object> upload(MultipartFile file)
	{		
		String pic = null;
		File path = null;
		File upload = null;
		try {
			// 获取跟目录
			path = new File(ResourceUtils.getURL("classpath:").getPath());
			if (!path.exists()) {
				path = new File("");
			}
			// 如果上传目录为/static/app/img/,则可以如下获取
			upload = new File("/home/find/web/img/");
			if (!upload.exists()) {
				upload.mkdirs();
			}
			if (!file.isEmpty()) {
				pic = FileUploadUtils.upload(upload.getAbsolutePath()+"/", file);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return successApp(pic);
	}
	
	/**
	 * 删除健康知识
	 */
	@RequiresPermissions("knowledge:remove")
	@Log(title = "健康知识", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(knowledgeService.deleteKnowledgeByIds(ids));
	}
	
}
